package longrise.phone.com.bjjt_henan.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;

import longrise.phone.com.bjjt_henan.R;

/**
 * 事故列表的适配器
 */
public class AccidentAdapter extends BaseAdapter {

    private Context mContext;

    private EntityBean[] lists;

    private int type;////0未完成；1已完成

    private View.OnClickListener listener;

    public AccidentAdapter(Context context, int type) {
        this.mContext = context;
        this.type = type;
    }

    public void setOnClickListner(View.OnClickListener listner) {
        this.listener = listner;
    }

    public void setData(EntityBean[] beans) {
        this.lists = beans;
    }

    @Override
    public int getCount() {
        return (lists == null) ? 0 : lists.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_accident_item, null);
            holder.typeIV = (ImageView) convertView.findViewById(R.id.viewstub_iv_type);
            holder.nameTV = (TextView) convertView.findViewById(R.id.accident_tv_name);
            holder.carnumTV = (TextView) convertView.findViewById(R.id.accident_tv_carnum);
            holder.timeTV = (TextView) convertView.findViewById(R.id.accident_tv_time);
            holder.dealLayout = (LinearLayout) convertView.findViewById(R.id.accident_layout_deal);
            holder.dealTV = (TextView) convertView.findViewById(R.id.accident_tv_deal);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置数据
        EntityBean bean = lists[position];
        //类型图片
        String state = bean.getString("state");
        holder.typeIV.setBackgroundResource(getDrawable(state));
        if (type == 0) {
            if (TextUtils.equals(state, "5") || TextUtils.equals(state, "6")
                    || TextUtils.equals(state, "15")) {
                holder.dealLayout.setVisibility(View.GONE);
            } else {
                holder.dealLayout.setVisibility(View.VISIBLE);
                holder.dealTV.setTag(bean);
                holder.dealTV.setOnClickListener(listener);
            }
        } else {
            holder.dealLayout.setVisibility(View.GONE);
        }
        //事故方
        String isSingle = bean.getString("casetype");
        if (TextUtils.equals("0", isSingle)) {
            holder.nameTV.setVisibility(View.GONE);
        } else {
            holder.nameTV.setVisibility(View.VISIBLE);
            String carnum = bean.getString("ocasecarno");
            if (TextUtils.isEmpty(carnum)) {
                carnum = "";
            }
            holder.nameTV.setText("事故方：" + carnum);
        }
        //我的车辆
        String mycarno = bean.getString("casecarno");
        if (TextUtils.isEmpty(mycarno)) {
            mycarno = "";
        }
        holder.carnumTV.setText("我的车辆：" + mycarno);
        //提交时间
        String ctime = bean.getString("casehaptime");
        if (TextUtils.isEmpty(ctime)) {
            ctime = "";
        } else {
            int index = ctime.indexOf(".");
            if (index > -1) {
                ctime = ctime.substring(0, index);
            }
        }
        holder.timeTV.setText("提交时间：" + ctime);

        return convertView;
    }

    //根据事故状态显示图片
    private int getDrawable(String state) {
        if (TextUtils.equals(state, "1")) {//待定责
            return R.mipmap.txt01;
        } else if (TextUtils.equals(state, "2")) {//待保险报案
            return R.mipmap.txt02;
        } else if (TextUtils.equals(state, "3")) {//待理赔
            return R.mipmap.txt03;
        } else if (TextUtils.equals(state, "4")) {//待评价
            return R.mipmap.txt04;
        } else if (TextUtils.equals(state, "5")) {//完成
            return R.mipmap.txt05;
        } else if (TextUtils.equals(state, "6")) {//撤销案件
            return R.mipmap.txt06;
        } else if (TextUtils.equals(state, "7")) {//定责图片待审
            return R.mipmap.txt07;
        } else if (TextUtils.equals(state, "8")) {//定损图片待审
            return R.mipmap.txt08;
        } else if (TextUtils.equals(state, "9")) {//案件信息未上传
            return R.mipmap.txt09;
        } else if (TextUtils.equals(state, "10")) {//未生成协议书
            return R.mipmap.txt10;
        } else if (TextUtils.equals(state, "11")) {//定损待审
            return R.mipmap.txt11;
        } else if (TextUtils.equals(state, "12")) {//未上传银行卡信息
            return R.mipmap.txt12;
        } else if (TextUtils.equals(state, "13")) {//定损照片未上传
            return R.mipmap.txt13;
        } else if (TextUtils.equals(state, "15")) {//放弃理赔
            return R.mipmap.txt15;
        } else if (TextUtils.equals(state, "16")) {//不属于在线定损
            return R.mipmap.txt16;
        }
        return R.mipmap.unknow;
    }

    private class ViewHolder {
        private ImageView typeIV;//事故类型图片
        private TextView nameTV;//事故方
        private TextView carnumTV;//我的车牌号
        private TextView timeTV;//提交时间
        private LinearLayout dealLayout;//继续处理视图
        private TextView dealTV;//继续处理
    }
}
